import Vue from "vue";
import VueRouter from 'vue-router'
import store from '@/store/store.js'
import {Message, Notification} from "element-ui";

Vue.use(VueRouter)
const routes = [
    {path: "/", component: () => import('@/views/Index.vue')},
    {path: "/login", component: () => import('@/views/Login.vue')},
    {path: "/register", component: () => import('@/views/Register.vue')},
    {path: "/detail", component: () => import('@/views/Detail.vue')},//meta: {requiresAuth: true}
    {path: "/cart", component: () => import('@/views/Cart.vue'), meta: {requiresAuth: true}},
    {path: "/order", component: () => import('@/views/Order.vue'), meta: {requiresAuth: true}},
    {path: "/pay", component: () => import('@/views/Pay.vue')},//meta: {requiresAuth: true}
    {path: "/search", component: () => import('@/views/Search.vue'),
        children: [
            {path: "/catsearch", component: () => import('@/components/search/CategorySearchMain.vue')},
            {path: "/keysearch", component: () => import('@/components/search/KeywordSearchMain.vue')}
        ]
    },//meta: {requiresAuth: true}
    {
        path: "/person",
        component: () => import('@/views/Person.vue'),
        redirect: '/index',
        meta: {requiresAuth: true},
        children: [{path: '/index', component: () => import('@/components/person/PersonIndex')},
            {path: '/orders', component: () => import('@/components/person/PersonOrder')},
            {path: '/comment',component: () => import('@/components/person/PersonComment')}]
    }

]
const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    //如果token存在证明用户登录过
    if (store.state.token) {
        //路径如果是请求的Login登录界面,直接跳回首页
        if (to.path == '/login') {
            next({path: '/'})
        } else {
            //是否有账号信息，如果没有获取用户信息
            if (store.state.account.length == 0) {
                //需要获取用户信息
                //alert('getUserInfo')
                store.dispatch('getUserInfo').then((res) => {
                    next();
                }).catch(error => {
                    // Message({
                    //     type: 'warning',
                    //     showClose: true,
                    //     message: '登录已过期'
                    // })
                    // localStorage.token=''
                    localStorage.removeItem('token')
                    next({path: '/login'});
                })
            } else {
                next();
            }
        }
    } else {
        if (to.matched.some(r => r.meta.requiresAuth)) {
            Message({
                type: 'warning',
                showClose: true,
                message: '请先进行登录'
            })
            Notification({
                title: '提示',
                message: '正在跳转到登录界面请稍等。。。',
                type: 'success'
            });
            setTimeout(() => {
                next({path: '/login', query: {redirect: to.fullPath}})
            }, 3000)
        } else {
            next();
        }
    }
})

export default router